﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Network
* Version: 0.5
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["network"] = new Object();
myPlugins["network"]["name"] = getLocalizedString2("mod_sysm_plugin_net_name");
myPlugins["network"]["onlyonce"] = false;
myPlugins["network"]["newObject"] = function (NewSettings) { return new plugin_network_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// Global Network

var NetworkAdapter = new Object();
	NetworkAdapter.id = new Array();
	NetworkAdapter.key = new Array();
	NetworkAdapter.name = new Array();
	NetworkAdapter.ip = new Array();
	NetworkAdapter.mac = new Array();
	NetworkAdapter.mtu = new Array();



	NetworkAdapter.valuesI = new Object();
	NetworkAdapter.valuesI.new = new Array();
	NetworkAdapter.valuesI.old = new Array();
	NetworkAdapter.valuesI.diff = new Array();
	NetworkAdapter.valuesI.peak = new Array();

	NetworkAdapter.valuesO = new Object();
	NetworkAdapter.valuesO.new = new Array();
	NetworkAdapter.valuesO.old = new Array();
	NetworkAdapter.valuesO.diff = new Array();
	NetworkAdapter.valuesO.peak = new Array();

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_network_sensor(NewSettings) {
	// Required variables
	this.plugin = "network";
	this.settings = NewSettings;
	this.edit = plugin_network_edit;
	this.ReDraw = plugin_network_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	var obj = this;


	// Init NetworkAdapter
	if(NetworkAdapter.id.length==0) plugin_network_GenerateDeviceList();
	if(NetworkAdapter.id.length==0) {
		alert("No active Network-Adapter found!");
		this.canceledplugin = true;
		return false;
	}


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolorI) this.settings.barcolorI = "#008F00";
	if(!this.settings.barcolorO) this.settings.barcolorO = "#8F0000";
	if(!this.settings.NetID) this.settings.NetID = NetworkAdapter.id[0];
	if(!this.settings.label) this.settings.label = "";
	if(!this.settings.labelip) this.settings.labelip = 0;
	if(!this.settings.labelipupdate) this.settings.labelipupdate = 1;
	if(!this.settings.labelright) this.settings.labelright = 0;
	if(!this.settings.stats) {
		this.settings.stats = new Object();
		this.settings.stats["hourly"] = new Object();
		this.settings.stats["hourly"].last = -1;
		this.settings.stats["hourly"].ValueI = 0;
		this.settings.stats["hourly"].ValueO = 0;
		this.settings.stats["hourly"].LastValueI = 0;
		this.settings.stats["hourly"].LastValueO = 0;
		this.settings.stats["daily"] = new Object();
		this.settings.stats["daily"].last = -1;
		this.settings.stats["daily"].ValueI = 0;
		this.settings.stats["daily"].ValueO = 0;
		this.settings.stats["daily"].LastValueI = 0;
		this.settings.stats["daily"].LastValueO = 0;
		this.settings.stats["weekly"] = new Object();
		this.settings.stats["weekly"].last = -1;
		this.settings.stats["weekly"].ValueI = 0;
		this.settings.stats["weekly"].ValueO = 0;
		this.settings.stats["weekly"].LastValueI = 0;
		this.settings.stats["weekly"].LastValueO = 0;
		this.settings.stats["monthly"] = new Object();
		this.settings.stats["monthly"].last = -1;
		this.settings.stats["monthly"].ValueI = 0;
		this.settings.stats["monthly"].ValueO = 0;
		this.settings.stats["monthly"].LastValueI = 0;
		this.settings.stats["monthly"].LastValueO = 0;
	}

	this.ArrID = -1;
	for(var i = 0;i < NetworkAdapter.id.length;i++)
		if( NetworkAdapter.id[i] == this.settings.NetID ) this.ArrID = i;


	//The Sensor
	this.networkI = new class_PercentBar(this.frame,0,0,"Network I");
	this.networkI.PercentColor(this.settings.color);
	this.networkI.PercentBarColor(this.settings.barcolorI);

	this.networkO = new class_PercentBar(this.frame,0,mod_sysmonitor_size,"Network O");
	this.networkO.PercentColor(this.settings.color);
	this.networkO.PercentBarColor(this.settings.barcolorO);

	plugin_network_draw.call(obj);

	this.networkO.text_name.onMultiClick = function ( ) { plugin_network_copytoclipboard.call(obj); };
	this.networkO.text_name.tooltip = getLocalizedString2("mod_sysm_plugin_net_copyip");

	// Update timer
	this.timer = new Timer();
	this.timer.interval = 1;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_network_update.call(obj); };
	plugin_network_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.networkO.background.vOffset + this.networkO.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions


	function plugin_network_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_incolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolorI;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_outcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolorO;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_device");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = NetworkAdapter.name;
		Drive_Edit_DialogFields[FieldID].optionValue = NetworkAdapter.id;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.NetID;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.label;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_net_pref_labeldesc");

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_text");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_net_pref_ip"),getLocalizedString2("mod_sysm_plugin_net_pref_mac"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.labelip;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_text_ipupdate");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.labelipupdate;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_text2");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_net_pref_cur"),getLocalizedString2("mod_sysm_plugin_net_pref_hourly"),getLocalizedString2("mod_sysm_plugin_net_pref_daily"),getLocalizedString2("mod_sysm_plugin_net_pref_weekly"),getLocalizedString2("mod_sysm_plugin_net_pref_monthly"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2,3,4);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.labelright;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolorI = Drive_Edit_DialogResults[1];
			this.settings.barcolorO = Drive_Edit_DialogResults[2];
			this.networkI.PercentColor(this.settings.color);
			this.networkO.PercentColor(this.settings.color);
			this.networkI.PercentBarColor(this.settings.barcolorI);
			this.networkO.PercentBarColor(this.settings.barcolorO);

			if(this.settings.NetID != Drive_Edit_DialogResults[3]) {
				this.settings.NetID = Drive_Edit_DialogResults[3];
				this.ArrID = -1;
				for(var i = 0;i < NetworkAdapter.id.length;i++)
					if( NetworkAdapter.id[i] == this.settings.NetID ) this.ArrID = i;
			}
			this.settings.label = Drive_Edit_DialogResults[4];
			this.settings.labelip = Drive_Edit_DialogResults[5];
			this.settings.labelipupdate = Drive_Edit_DialogResults[6];
			this.settings.labelright = Drive_Edit_DialogResults[7];

			plugin_network_draw.call(obj);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_network_update() {
		if(this.ArrID!=-1) {
			plugin_network_GetDeviceValues.call(obj);

			// Cumulative Values
			var currentTime = new Date();

			if(currentTime.getHours() != this.settings.stats["hourly"].last) {
				this.settings.stats["hourly"].last = currentTime.getHours();
				this.settings.stats["hourly"].LastValueI = this.settings.stats["hourly"].ValueI;
				this.settings.stats["hourly"].LastValueO = this.settings.stats["hourly"].ValueO;
				this.settings.stats["hourly"].ValueI = 0;
				this.settings.stats["hourly"].ValueO = 0;
			}
			this.settings.stats["hourly"].ValueI += NetworkAdapter.valuesI.diff[this.ArrID];
			this.settings.stats["hourly"].ValueO += NetworkAdapter.valuesO.diff[this.ArrID];

			if(currentTime.getDate() != this.settings.stats["daily"].last) {
				this.settings.stats["daily"].last = currentTime.getDate();
				this.settings.stats["daily"].LastValueI = this.settings.stats["daily"].ValueI;
				this.settings.stats["daily"].LastValueO = this.settings.stats["daily"].ValueO;
				this.settings.stats["daily"].ValueI = 0;
				this.settings.stats["daily"].ValueO = 0;
			}
			this.settings.stats["daily"].ValueI += NetworkAdapter.valuesI.diff[this.ArrID];
			this.settings.stats["daily"].ValueO += NetworkAdapter.valuesO.diff[this.ArrID];

			if(   currentTime.getDay() == 1 && currentTime.getDay() != this.settings.stats["weekly"].last     ) {
				this.settings.stats["weekly"].LastValueI = this.settings.stats["weekly"].ValueI;
				this.settings.stats["weekly"].LastValueO = this.settings.stats["weekly"].ValueO;
				this.settings.stats["weekly"].ValueI = 0;
				this.settings.stats["weekly"].ValueO = 0;
			}
			this.settings.stats["weekly"].last = currentTime.getDay();
			this.settings.stats["weekly"].ValueI += NetworkAdapter.valuesI.diff[this.ArrID];
			this.settings.stats["weekly"].ValueO += NetworkAdapter.valuesO.diff[this.ArrID];

			if(   currentTime.getMonth() != this.settings.stats["monthly"].last     ) {
				this.settings.stats["monthly"].last = currentTime.getMonth();
				this.settings.stats["monthly"].LastValueI = this.settings.stats["monthly"].ValueI;
				this.settings.stats["monthly"].LastValueO = this.settings.stats["monthly"].ValueO;
				this.settings.stats["monthly"].ValueI = 0;
				this.settings.stats["monthly"].ValueO = 0;
			}
			this.settings.stats["monthly"].ValueI += NetworkAdapter.valuesI.diff[this.ArrID];
			this.settings.stats["monthly"].ValueO += NetworkAdapter.valuesO.diff[this.ArrID];


			//Set Peak Value
			if( NetworkAdapter.valuesI.diff[this.ArrID] > NetworkAdapter.valuesI.peak[this.ArrID] ) NetworkAdapter.valuesI.peak[this.ArrID] = NetworkAdapter.valuesI.diff[this.ArrID];
			if( NetworkAdapter.valuesO.diff[this.ArrID] > NetworkAdapter.valuesO.peak[this.ArrID] ) NetworkAdapter.valuesO.peak[this.ArrID] = NetworkAdapter.valuesO.diff[this.ArrID];


			//Update
			switch (this.settings.labelright) {
				case "1":
					this.networkI.PercentBarValue(NetworkAdapter.valuesI.diff[this.ArrID] / NetworkAdapter.valuesI.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["hourly"].ValueI   ) );
					this.networkO.PercentBarValue(NetworkAdapter.valuesO.diff[this.ArrID] / NetworkAdapter.valuesO.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["hourly"].ValueO  ) );
				break;
				case "2":
					this.networkI.PercentBarValue(NetworkAdapter.valuesI.diff[this.ArrID] / NetworkAdapter.valuesI.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["daily"].ValueI   ) );
					this.networkO.PercentBarValue(NetworkAdapter.valuesO.diff[this.ArrID] / NetworkAdapter.valuesO.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["daily"].ValueO  ) );
				break;
				case "3":
					this.networkI.PercentBarValue(NetworkAdapter.valuesI.diff[this.ArrID] / NetworkAdapter.valuesI.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["weekly"].ValueI   ) );
					this.networkO.PercentBarValue(NetworkAdapter.valuesO.diff[this.ArrID] / NetworkAdapter.valuesO.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["weekly"].ValueO  ) );
				break;
				case "4":
					this.networkI.PercentBarValue(NetworkAdapter.valuesI.diff[this.ArrID] / NetworkAdapter.valuesI.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["monthly"].ValueI   ) );
					this.networkO.PercentBarValue(NetworkAdapter.valuesO.diff[this.ArrID] / NetworkAdapter.valuesO.peak[this.ArrID] * 100, bytesToUIString(  this.settings.stats["monthly"].ValueO  ) );
				break;
				default:
					this.networkI.PercentBarValue(NetworkAdapter.valuesI.diff[this.ArrID] / NetworkAdapter.valuesI.peak[this.ArrID] * 100, bytesToUIString(  NetworkAdapter.valuesI.diff[this.ArrID]   ) );
					this.networkO.PercentBarValue(NetworkAdapter.valuesO.diff[this.ArrID] / NetworkAdapter.valuesO.peak[this.ArrID] * 100, bytesToUIString(  NetworkAdapter.valuesO.diff[this.ArrID]  ) );
				break;
			}


			// Update IP-Adress
			if(this.settings.labelip==0 && this.settings.labelipupdate==1) {
				if(system.platform=="windows") {
					NetworkAdapter.ip[this.ArrID] = plugin_network_GetDeviceIP(NetworkAdapter.id[this.ArrID])
				} else if(system.platform=="macintosh") {
					//Todo
				}
				if(this.networkO.text_name.data != NetworkAdapter.ip[this.ArrID]) this.networkO.text_name.data = NetworkAdapter.ip[this.ArrID];
			}


		} else {
			print("Error: ArrID is -1");
		}
		return true;
	}


	function plugin_network_draw() {
		if(this.ArrID!=-1) {
			if(this.settings.label!="")
				this.networkI.text_name.data = this.settings.label.substr(0,16);
			else
				this.networkI.text_name.data = NetworkAdapter.name[this.ArrID].substr(0,16);

			if(this.settings.labelip==0)
				this.networkO.text_name.data = NetworkAdapter.ip[this.ArrID];
			else
				this.networkO.text_name.data = NetworkAdapter.mac[this.ArrID];
		} else {
			this.networkI.text_name.data = " + " + this.settings.NetID + " + ";
			this.networkO.text_name.data = ". . .";
		}
		return true;
	}


	function plugin_network_GenerateDeviceList() {
		if(system.platform=="windows") {
if(mod_sysmonitor_debug>1) print("*** network: GenerateDeviceList");
			if(NetworkAdapter.id.length==0) {
if(mod_sysmonitor_debug>1) print("*** network: GenerateDeviceList * Need to refresh");
				if(VBSEnabled) NetworkAdapter.id = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_network + '"').split("\n");
				else NetworkAdapter.id = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "Index" "2" "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 1"').split("\n");
if(mod_sysmonitor_debug>1) {
	var tmp = "";
	for(var i=0;i<NetworkAdapter.id.length;i++)
		tmp += NetworkAdapter.id[i] + ",";
	print("*** network: " + tmp);
}
				for(var i = 0 ; i < NetworkAdapter.id.length ; i++ ) {
					try {
						var tmp = wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + NetworkAdapter.id[i]).Properties_.Item("Description").Value;
						tmp = tmp.replace(/\//g, "_");
						tmp = tmp.replace(/\(/g, "[");
						tmp = tmp.replace(/\)/g, "]");
						tmp = tmp.replace(/#/g, "_");
						tmp = tmp.replace(/\™/g, "[TM]");
						NetworkAdapter.key[i] = tmp;
						if(plugin_network_TestDevice(tmp)) {
							NetworkAdapter.name[i] = wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + NetworkAdapter.id[i]).Properties_.Item("Description").Value;
							//NetworkAdapter.name[i] = wmi.Get('Win32_NetworkAdapter.DeviceID=' + NetworkAdapter.id[i]).Properties_.Item("NetConnectionID").Value;
						} else {
							NetworkAdapter.name[i] = "* " + wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + NetworkAdapter.id[i]).Properties_.Item("Description").Value;
							//NetworkAdapter.name[i] = "* " + wmi.Get('Win32_NetworkAdapter.DeviceID=' + NetworkAdapter.id[i]).Properties_.Item("NetConnectionID").Value;
						}
						NetworkAdapter.ip[i] = plugin_network_GetDeviceIP(NetworkAdapter.id[i])
						NetworkAdapter.mac[i] = plugin_network_GetDeviceMAC(NetworkAdapter.id[i]);
						NetworkAdapter.valuesI.peak[i] = 0;
						NetworkAdapter.valuesO.peak[i] = 0;
						if(mod_sysmonitor_debug>1) print("*** network: Found: " + NetworkAdapter.key[i] + " *** " + NetworkAdapter.ip[i] + " *** " + NetworkAdapter.mac[i] + " *** " + NetworkAdapter.name[i]);
					} catch (e) {
						print("WMI Error (1st Get): [" + i + "/" + NetworkAdapter.id[i] + "]" + e);
						NetworkAdapter.id[i] = -1;
						NetworkAdapter.name[i] = "* Unusable Adapter!";
						NetworkAdapter.ip[i] = plugin_network_GetDeviceIP(NetworkAdapter.id[i])
						NetworkAdapter.mac[i] = plugin_network_GetDeviceMAC(NetworkAdapter.id[i]);
						NetworkAdapter.valuesI.peak[i] = 0;
						NetworkAdapter.valuesO.peak[i] = 0;
					}
				}
			}
		} else if(system.platform=="macintosh") {
			var tmp;
			var i;

			var mac_GetInterfaces = runCommand("netstat -i");
			mac_GetInterfaces = mac_GetInterfaces.split("\n");
			for(i=1;i<mac_GetInterfaces.length;i++) {
				tmp = mac_GetInterfaces[i].replace(/  /g, " ");
				while(tmp.lastIndexOf("  ")>-1)
					tmp = tmp.replace(/  /g, " ");
				tmp = tmp.split(" ");
				if(parseInt(tmp[4])>0) {
					if(NetworkAdapter.key[tmp[0]]==undefined) {
						print("tmp [" + tmp[0] + "] [" + tmp[1] + "] [" + tmp[2] + "] [" + tmp[3] + "] [" + tmp[4] + "]");
						NetworkAdapter.id[NetworkAdapter.id.length] = tmp[0];
						NetworkAdapter.key[tmp[0]] = NetworkAdapter.id.length-1;
						NetworkAdapter.mtu[NetworkAdapter.id.length-1] = parseInt(tmp[1]); // MTU
						NetworkAdapter.valuesI.peak[NetworkAdapter.id.length-1] = 0;
						NetworkAdapter.valuesO.peak[NetworkAdapter.id.length-1] = 0;
						if(tmp[2]=="localhost")
							NetworkAdapter.name[NetworkAdapter.id.length-1] = "Localhost (" + tmp[0] + ")";
							NetworkAdapter.mac[NetworkAdapter.id.length-1] = "00:00:00:00:00:00";
							NetworkAdapter.ip[NetworkAdapter.id.length-1] = "127.0.0.1";
					}
				}
			}

			var tmpPList = parse_plist(runCommand("system_profiler -xml SPNetworkDataType"));
			for(i=0;i<tmpPList[0]._items.length;i++) {
				if(NetworkAdapter.key[tmpPList[0]._items[i].interface]!=undefined) {
					if(mod_sysmonitor_debug) print("tmpPList ++ : " + tmpPList[0]._items[i].interface + " : " + tmpPList[0]._items[i]._name + " : " + tmpPList[0]._items[i].Ethernet['MAC Address'] + " : " + tmpPList[0]._items[i].ip_address);
					NetworkAdapter.name[NetworkAdapter.key[tmpPList[0]._items[i].interface]] = tmpPList[0]._items[i]._name + " (" + tmpPList[0]._items[i].interface + ")";
					NetworkAdapter.ip[NetworkAdapter.key[tmpPList[0]._items[i].interface]] = tmpPList[0]._items[i].ip_address[0];
					NetworkAdapter.mac[NetworkAdapter.key[tmpPList[0]._items[i].interface]] = tmpPList[0]._items[i].Ethernet['MAC Address'];
				} else {
					if(mod_sysmonitor_debug) print("tmpPList -- : " + tmpPList[0]._items[i].interface + " : " + tmpPList[0]._items[i]._name);
				}
			}
		}
		return true;
	}


	function plugin_network_TestDevice(DeviceName) {
		if(system.platform=="windows") {
			try {
				var my_test = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + DeviceName + '"' ).Properties_.Item( "BytesSentPerSec" ).Value;
				if(isNaN(my_test))
					return false;
				else
					return true;
			} catch (e) { print("WMI Error (TestDevice): " + e); return false; }
		} else if(system.platform=="macintosh") {
			// No need for Mac, already done in GetDeviceList.
		}
	}



	function plugin_network_GetDeviceIP(DeviceID) {
		if(system.platform=="windows") {
			try {
				return wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + DeviceID).Properties_.Item("IPAddress").Value.getItem(0);
			} catch (e) { print("WMI Error (GetDeviceIP): " + e); return "0.0.0.0"; }
		} else if(system.platform=="macintosh") {
			// No need for Mac, already done in GetDeviceList.
		}
	}


	function plugin_network_GetDeviceMAC(DeviceID) {
		if(system.platform=="windows") {
			try {
				return wmi.Get('Win32_NetworkAdapterConfiguration.Index=' + DeviceID).Properties_.Item("MACAddress").Value;
			} catch (e) { print("WMI Error (GetDeviceMAC): " + e); return "00:00:00:00:00:00"; }
		} else if(system.platform=="macintosh") {
			// No need for Mac, already done in GetDeviceList.
		}
	}


	function plugin_network_GetDeviceValues() {
		if(system.platform=="windows") {
			NetworkAdapter.valuesI.new[this.ArrID] = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + NetworkAdapter.key[this.ArrID] + '"' ).Properties_.Item( "BytesReceivedPerSec" ).Value;
			if(!NetworkAdapter.valuesI.old[this.ArrID]) NetworkAdapter.valuesI.old[this.ArrID] = NetworkAdapter.valuesI.new[this.ArrID];
			NetworkAdapter.valuesI.diff[this.ArrID] = (NetworkAdapter.valuesI.new[this.ArrID] - NetworkAdapter.valuesI.old[this.ArrID]) / this.timer.interval;
			if(NetworkAdapter.valuesI.diff[this.ArrID]<0) NetworkAdapter.valuesI.diff[this.ArrID] = ((NetworkAdapter.valuesI.new[this.ArrID] + 4294967296) - NetworkAdapter.valuesI.old[this.ArrID]) / this.timer.interval;
			NetworkAdapter.valuesI.old[this.ArrID] = NetworkAdapter.valuesI.new[this.ArrID];

			NetworkAdapter.valuesO.new[this.ArrID] = wmi.Get( 'Win32_PerfRawData_Tcpip_NetworkInterface.name="' + NetworkAdapter.key[this.ArrID] + '"' ).Properties_.Item( "BytesSentPerSec" ).Value;
			if(!NetworkAdapter.valuesO.old[this.ArrID]) NetworkAdapter.valuesO.old[this.ArrID] = NetworkAdapter.valuesO.new[this.ArrID];
			NetworkAdapter.valuesO.diff[this.ArrID] = (NetworkAdapter.valuesO.new[this.ArrID] - NetworkAdapter.valuesO.old[this.ArrID]) / this.timer.interval;
			if(NetworkAdapter.valuesO.diff[this.ArrID]<0) NetworkAdapter.valuesO.diff[this.ArrID] = ((NetworkAdapter.valuesO.new[this.ArrID] + 4294967296) - NetworkAdapter.valuesO.old[this.ArrID]) / this.timer.interval;
			NetworkAdapter.valuesO.old[this.ArrID] = NetworkAdapter.valuesO.new[this.ArrID];
		} else if(system.platform=="macintosh") {
			var mac_GetInterfaces = runCommand("netstat -I " + NetworkAdapter.id[this.ArrID] + " -b | head -2");
			mac_GetInterfaces = mac_GetInterfaces.split("\n");

			tmp = mac_GetInterfaces[1].replace(/  /g, " ");
			while(tmp.lastIndexOf("  ")>-1)
				tmp = tmp.replace(/  /g, " ");
			tmp = tmp.split(" ");
			//print("netstat #" + i + " [" + tmp[0] + "] " + tmp[tmp.length - 5] + "/" + tmp[tmp.length - 2] + " : " + tmp.length);
		
			NetworkAdapter.valuesI.new[NetworkAdapter.key[tmp[0]]] = parseInt(tmp[tmp.length - 5]);
			if(!NetworkAdapter.valuesI.old[NetworkAdapter.key[tmp[0]]]) NetworkAdapter.valuesI.old[NetworkAdapter.key[tmp[0]]] = NetworkAdapter.valuesI.new[NetworkAdapter.key[tmp[0]]];
			NetworkAdapter.valuesI.diff[NetworkAdapter.key[tmp[0]]] = (NetworkAdapter.valuesI.new[NetworkAdapter.key[tmp[0]]] - NetworkAdapter.valuesI.old[NetworkAdapter.key[tmp[0]]]) / this.timer.interval;
			NetworkAdapter.valuesI.old[NetworkAdapter.key[tmp[0]]] = NetworkAdapter.valuesI.new[NetworkAdapter.key[tmp[0]]];
		
			NetworkAdapter.valuesO.new[NetworkAdapter.key[tmp[0]]] = parseInt(tmp[tmp.length - 2]);
			if(!NetworkAdapter.valuesO.old[NetworkAdapter.key[tmp[0]]]) NetworkAdapter.valuesO.old[NetworkAdapter.key[tmp[0]]] = NetworkAdapter.valuesO.new[NetworkAdapter.key[tmp[0]]];
			NetworkAdapter.valuesO.diff[NetworkAdapter.key[tmp[0]]] = (NetworkAdapter.valuesO.new[NetworkAdapter.key[tmp[0]]] - NetworkAdapter.valuesO.old[NetworkAdapter.key[tmp[0]]]) / this.timer.interval;
			NetworkAdapter.valuesO.old[NetworkAdapter.key[tmp[0]]] = NetworkAdapter.valuesO.new[NetworkAdapter.key[tmp[0]]];
		}
		return true;
	}

	function plugin_network_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		this.networkI.style = preferences.mod_sysmonitor_bar_style.value;
		this.networkI.size = mod_sysmonitor_size;
		this.networkO.style = preferences.mod_sysmonitor_bar_style.value;
		this.networkO.size = mod_sysmonitor_size;
		this.networkO.vOffset = mod_sysmonitor_size;

		// Re-Calculating sensore height (with background)
		this.frame.height = this.networkO.background.vOffset + this.networkO.background.height;

		return true;
	}

	function plugin_network_copytoclipboard() {
		system.clipboard = this.networkO.text_name.data;
		this.networkO.text_name.opacity = 0;
		var a = new FadeAnimation( this.networkO.text_name, 255, 350, animator.kEaseIn );
		animator.start( a ) ;
	}

}



// --------------------------------------------------------------------------------
// The END